﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CadDotNetTools;

namespace LineTools
{
    public static class MyLine
    {
        /// <summary>
        /// 直线
        /// </summary>
        /// <param name="doc">Document</param>
        public static void Lines(this Document doc)
        {
            var db = doc.Database;
            var ed = doc.Editor;
            //声明一个直线的集合对象
            List<ObjectId> lineList = new List<ObjectId>();
            //声明一个预备的对象
            Point3d pointStart = new Point3d(100, 100, 0);
            Point3d pointPre = new Point3d(100, 100, 0);

            PromptPointResult ppr = ed.GetPoint("\n指定第一个点");
            if (ppr.Status == PromptStatus.Cancel)
            {
                return;
            }
            if (ppr.Status == PromptStatus.None)
            {
                pointPre = pointStart;
            }
            if (ppr.Status == PromptStatus.OK)
            {
                pointStart = ppr.Value;
                pointPre = pointStart;
            }
            LineJig lineJig;
            PromptResult pr;
            //判断循环是否继续
            bool isContinue = true;
            while (isContinue)
            {
                //只要有两条直线就闭合
                if (lineList.Count > 1)
                {
                    //ppr = ed.GetPoint("\n指定下一点或 [闭合(C)/放弃(U)]:", pointPre, new string[] { "C", "U" });
                    lineJig = new LineJig(pointPre, "\n指定下一点或 [闭合(C)/放弃(U)]:", new string[] { "C", "U" });
                    pr = ed.Drag(lineJig);

                }
                else
                {
                    //ppr = ed.GetPoint("\n指定下一点或 [放弃(U)]:", pointPre, new string[] { "U" });
                    lineJig = new LineJig(pointPre, "\n指定下一点或 [放弃(U)]:", new string[] { "U" });
                    pr = ed.Drag(lineJig);
                }

                //Point3d pointNext;
                if (pr.Status == PromptStatus.Cancel)
                {
                    return;
                }
                if (pr.Status == PromptStatus.None)
                {
                    return;
                }
                if (pr.Status == PromptStatus.OK)
                {
                    //pointNext = ppr.Value;
                    //lineList.Add(db.AddLineToModelSpace(pointPre, pointNext));
                    //pointPre = pointNext;
                    Line line = (Line)lineJig.GetEntity();
                    pointPre = line.EndPoint;
                    ObjectId lId = db.AddEntityToModelSpace(line);
                    lineList.Add(lId);
                }
                if (pr.Status == PromptStatus.Keyword)
                {
                    switch (pr.StringResult)
                    {
                        case "U":
                            if (lineList.Count == 0)
                            {
                                pointStart = new Point3d(100, 100, 0);
                                pointPre = new Point3d(100, 100, 0);

                                ppr = ed.GetPoint("\n指定第一个点：");
                                if (ppr.Status == PromptStatus.Cancel)
                                {
                                    return;
                                }
                                if (ppr.Status == PromptStatus.None)
                                {
                                    pointPre = pointStart;
                                }
                                if (ppr.Status == PromptStatus.OK)
                                {
                                    pointStart = ppr.Value;
                                    pointPre = pointStart;
                                }
                            }
                            else if (lineList.Count > 0)
                            {
                                int count = lineList.Count;
                                ObjectId lId = lineList.ElementAt(count - 1);
                                pointPre = lId.GetLineStartPoint();
                                lineList.RemoveAt(count - 1);
                                lId.EraseEntity();
                            }
                            break;
                        case "C":
                            lineList.Add(db.AddEntityToModelSpace(new Line(pointPre, pointStart)));
                            isContinue = false;
                            //return;
                            break;
                        case " ":
                            isContinue = false;
                            break;

                        default:
                            break;
                    }
                }
            }
        }
    }
}
